package com.tpl.core.basedata.action.sys;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.tpl.core.action.BaseAction;
import com.tpl.core.service.DataDictService;
import com.tpl.core.basedata.service.DictMenuService;
import com.tpl.core.common.DateUtil;
import com.tpl.core.common.ValidateUtil;
import com.tpl.core.common.exception.RuleException;
import com.tpl.core.common.page.Page;
import com.tpl.core.entity.sys.DataDict;
import com.tpl.core.entity.sys.DictMenu;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 说明 :数据字典视图层
 * @author 
 * @version
 *
 */
@Controller
@RequestMapping("/dict")
public class DataDictAction extends BaseAction {
	private static final long serialVersionUID = 2015250479865884134L;

	@Autowired
	private DataDictService dataDictService;
	@Autowired
	private DictMenuService dictMenuService;
	
	@RequestMapping("/toDictManager.do")
	public String toDictManager(){
		return "dict/dictManager";
	}
	@RequestMapping("/toDictList.do")
	public String toDictList(String key,String dictName){
		Page page=dataDictService.getDataDictList(key, dictName,getPageIndex(), getPageSize());
		List<DictMenu> menus=dictMenuService.getDictMenuList(new DictMenu());
		putInRequest("page", page,"key",key,"dictName",dictName,"menus",menus);
		return "dict/dict_list";
	}
	@RequestMapping("/toDictList/{key}.do")
	public String toDicMenutList(@PathVariable String key,String dictName){
		if(StringUtils.isBlank(key))throw new RuntimeException("目录标识不能为空");
		DataDict dataDict = dataDictService.getDictByKeyAndCode("base.setting",key);
		if(dataDict==null) {
			throw new RuleException("未找到对应的配置");
		}
		List<DataDict> dictSetting = dataDictService.getDictMenusByKey(key);
		JSONObject jo = new JSONObject();
		for(DataDict dd : dictSetting)
			jo.put(dd.getCode(),dd.getValue());
		Page page=dataDictService.getDataDictList(key+"_setting_data",getPageIndex(), getPageSize());
//		List<DictMenu> menus=dictMenuService.getDictMenuList(new DictMenu());
		putInRequest("page", page,"dictName",dictName,"menus",dataDict,"dictSetting",jo);
		return "dict/dict_list2";
	}
	
	@RequestMapping("/toEditDict.do")
	public String getDataDictById(Long dictId){
		List<DictMenu> dictMenus=dataDictService.getDictMenus();
		if(null!=dictId){
			DataDict dict=dataDictService.getDictById(dictId);
			putInRequest("dict", dict);
		}else {
			putInRequest("dict", new DataDict());
		}
		putInRequest("dictMenus", dictMenus);
		return "dict/dictEdit";
	}
	@RequestMapping("/toEditDict/{key}.do")
	public String toEditDict(@PathVariable String key,Long dictId){
		if(StringUtils.isBlank(key))throw new RuntimeException("目录标识不能为空");
		DataDict dataDict = dataDictService.getDictByKeyAndCode("base.setting",key);
		if(dataDict==null) {
			throw new RuleException("未找到对应的配置");
		}
		List<DataDict> dictSetting = dataDictService.getDictMenusByKey(key);
		JSONObject jo = new JSONObject();
		for(DataDict dd : dictSetting)
			jo.put(dd.getCode(),dd.getValue());
		if(null!=dictId){
			DataDict dict=dataDictService.getDictById(dictId);
			putInRequest("dict", dict);
		}else {
			putInRequest("dict", new DataDict());
		}
		putInRequest("menus", dataDict,"dictSetting",jo);
		return "dict/dictEdit2";
	}
	
	/**
	 * 数据字典保存
	 * @return
	 * @return 
	 */
	@RequestMapping("/saveOrUpdateDict.do")
	public  @ResponseBody void doSaveOrUpdate(DataDict dataDict){
		dataDict.setUpdateDate(DateUtil.now());
		dataDict.setUpdator(getLoginUser().getUserNo());
		dataDictService.saveOrUpdateDict(dataDict);
		outJsonSuccess();
	}
	/**
	 * 数据字典删除
	 * @return
	 */
	@RequestMapping("/toDelDict.do")
	public @ResponseBody void doDeleteOst(Long dictId){
		ValidateUtil.validRequired(dictId, "字典信息");
		dataDictService.deleteDataDict(dictId);
		outJsonSuccess();
	}
	/**
	 * 数据字典删除
	 * @return
	 */
	@RequestMapping("/toDelDict/{key}.do")
	public @ResponseBody void doDeleteOst(@PathVariable String key,Long dictId){
		if(StringUtils.isBlank(key))throw new RuntimeException("目录标识不能为空");
		DataDict dataDict = dataDictService.getDictByKeyAndCode("base.setting",key);
		if(dataDict==null) {
			throw new RuleException("未找到对应的配置");
		}
		ValidateUtil.validRequired(dictId, "字典信息");
		dataDictService.deleteDataDict(dictId);
		outJsonSuccess();
	}
	
}
